package cn.wikiflyer.beikang.act.user;

import net.tsz.afinal.annotation.view.ViewInject;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.utils.WKCountDownUtils;
import cn.wk.libs4a.utils.WKUtils;
import cn.wk.libs4a.view.WKHeader;

/**
 * 更换手机号/更换绑定手机号——当前号码
 * @author qunlee_mr
 *
 */

/**
 * 不行，这里逻辑待定
 */
public class UserChangePhoneAct extends BaseActivity {
	
	@ViewInject(id = R.id.header)
	private WKHeader header;
	@ViewInject(id = R.id.changetel_next, click = "onClick")
	private Button nextBtn;
	@ViewInject(id = R.id.changetel_codebtn, click = "onClick")
	private Button codeBtn;
	
	@ViewInject(id = R.id.changetel_note)
	private TextView noteText;
	@ViewInject(id = R.id.changetel_tel)
	private EditText telEdit;
	@ViewInject(id = R.id.changetel_code)
	private EditText codeEdit;
	
	private boolean is_next = false;	//默认为当前号码下一步

	private WKCountDownUtils cdUtils = null;
	
	private String tel_now = "";
	private String tel_new = "";
	
	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_changephone);
	} 

	@Override
	public void init() {
		changeTextAction();	//初始化标题等Text
		header.setClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (is_next) {
					is_next = false;
					tel_new = telEdit.getText().toString();
					changeTextAction();
				} else {
					finish();
				}
			}
		}, null, null);
	}

	@Override
	public void setData() {
		cdUtils = new WKCountDownUtils(codeBtn, "获取验证码", 20, 1);
	}
	
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.changetel_codebtn:
			nextAction(R.id.changetel_codebtn);
			break;
		case R.id.changetel_next:
			nextAction(R.id.changetel_next);
			break;

		default:
			break;
		}
	}
	
	//下一步 or 确定 按钮
	private void nextAction(int id) {
		if (TextUtils.isEmpty(telEdit.getText())) {
			WKApplication.showToast("请输入手机号");
			return;
		}
		if (!WKUtils.isTel(telEdit.getText().toString())) {
			WKApplication.showToast("手机号码格式不正确");
			return;
		}
		if (id == R.id.changetel_next){		//提交按钮
			if (TextUtils.isEmpty(codeEdit.getText())) {
				WKApplication.showToast("请输入验证码");
				return;
			}
			//下一步 or 提交
			if (!is_next) {
				//验证通过后
				is_next = true;
				tel_now = telEdit.getText().toString();
				changeTextAction();
			} else {
				//提交服务器
				//确定 修改密码后——放在提交服务器之后
				WKApplication.showToast("修改成功");
				is_next = false;
			}
		} else {							//获取验证码按钮
			cdUtils.start();
			//获取验证码
		}
	}
	
	//初始化标题等Text
	private void changeTextAction() {
		if (cdUtils != null)
			cdUtils.stop("获取验证码");
		header.setTitle(is_next?"验证新号码":"修改绑定号码");
		nextBtn.setText(is_next?"确定":"下一步");
		noteText.setText(is_next?"验证新号码":"验证当前号码");
		telEdit.setText(is_next?tel_new:tel_now);
		codeEdit.setText("");
		
//		telEdit.setFocusable(true);
//		telEdit.setFocusableInTouchMode(true);
		telEdit.requestFocus();
		telEdit.setSelection(is_next?tel_new.length():tel_now.length());
	}

}
